<template>
  <div class="import">
    <h3>批量导入</h3>
    <myexecl :beforeUpdate="beforeUpdate" :onSuccess="onSuccess"></myexecl>
  </div>
</template>

<script>
import{addAllUserApi} from '@/api/employees'
export default {
  methods:{
    beforeUpdate(file){
      const less2M=file.size / 1024 / 1024 < 2
      if(!less2M){
        this.$message.error('文件大小不得超过 2M')
      }
      return less2M
    },
    FormDate(days){
      const mytime=(days-1)*24*60*60*1000
      const mydate=new Date(mytime)
      mydate.setFullYear(mydate.getFullYear()-70)
      return mydate
    },
    async  onSuccess({header,results}){
      const obj={
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      }
      const newArr=results.map(item=>{
        const newObj={}
        //Object.keys将对象中的键转化为数组
        Object.keys(obj).forEach(value=>{
          newObj[obj[value]]=this.FormDate(item[value])
        })
        return newObj
        // console.log(newObj);
      })
      //数据提交
      await addAllUserApi(newArr)
      //提示信息
      this.$message.success('上传成功')
      //返回员工页面
      this.$router.push('/employees')
    }
  }

}
</script>

<style lang="scss">
.import {
  h3 {
    text-align: center;
  }
}
</style>